/*
 * Copyright (c) 2010-2020 Belledonne Communications SARL.
 *
 * This file is part of linphone-android
 * (see https://www.linphone.org).
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package com.newlink.building.activities.main.settings.viewmodels

import android.util.Log
import androidx.lifecycle.MutableLiveData
import com.newlink.building.NL_App
import com.newlink.building.activities.main.settings.Module_Phone_SettingListenerStub
import com.newlink.building.activities.main.viewmodels.Module_Phone_LogsUploadViewModel
import org.linphone.core.Factory
import org.linphone.core.LogLevel

class Module_Phone_LogSettingsViewModel() : Module_Phone_LogsUploadViewModel() {

    private val prefs = NL_App.corePreferences
    private val core = NL_App.coreContext.core

    var uploadErrorTmp: Boolean = false

    val uploadErrorListener = object : Module_Phone_SettingListenerStub() {
        override fun onBoolValueChanged(newValue: Boolean) {
            uploadError.value = newValue
        }
    }

    val uploadError = MutableLiveData<Boolean>()

    val debugModeListener = object : Module_Phone_SettingListenerStub() {
        override fun onBoolValueChanged(newValue: Boolean) {
            prefs.debugLogs = newValue
            val logLevel = if (newValue) LogLevel.Message else LogLevel.Error
            Factory.instance().loggingService.setLogLevel(logLevel)
        }
    }

    val debugMode = MutableLiveData<Boolean>()

    private val logLevelValues = arrayListOf(1, 2, 4, 8, 16, 32)
//    val logLevelLabels = arrayListOf(1, 2, 3, 4, 5, 6)
//    val logLevelIndex = MutableLiveData<Int>()

    val logLevelIndex = MutableLiveData<Int>()
    val logLevelLabels = MutableLiveData<ArrayList<String>>()

    val logLevelListener = object : Module_Phone_SettingListenerStub() {
        override fun onListValueChanged(position: Int) {

            val level = when (position) {
                1 -> LogLevel.Debug
                2 -> LogLevel.Trace
                4 -> LogLevel.Message
                8 -> LogLevel.Warning
                16 -> LogLevel.Error
                32 -> LogLevel.Fatal
                else -> LogLevel.Debug
            }
            prefs.logLevel = logLevelValues[position]
            Factory.instance().loggingService.setLogLevel(level)
        }
    }

    private fun initLogLevelList() {
        val labels = arrayListOf<String>()
        for ((index, _) in logLevelValues.withIndex()) {
            labels.add("$index")
        }
        logLevelLabels.value = labels
        logLevelIndex.value = prefs.logLevel
        val currentValue = prefs.logLevel
        Log.e("jake", "$currentValue")
        logLevelIndex.value = logLevelValues.indexOf(currentValue)
    }

    val logsServerUrlListener = object : Module_Phone_SettingListenerStub() {
        override fun onTextValueChanged(newValue: String) {
            core.logCollectionUploadServerUrl = newValue
        }
    }
    val logsServerUrl = MutableLiveData<String>()

    val sendDebugLogsListener = object : Module_Phone_SettingListenerStub() {
        override fun onClicked() {
            Log.e("jake", "-------sendDebugLogsListener-------")

            uploadLogs()
        }
    }

    val resetDebugLogsListener = object : Module_Phone_SettingListenerStub() {
        override fun onClicked() {
            Log.e("jake", "-------resetDebugLogsListener-------")
            resetLogs()
        }
    }

    init {
        debugMode.value = prefs.debugLogs
        uploadError.value = prefs.uploadLogs
        uploadErrorTmp = uploadError.value!!

        logsServerUrl.value = core.logCollectionUploadServerUrl
        initLogLevelList()
    }

    fun savePreferenceChanged() {
        if (uploadErrorTmp != uploadError.value!!) {
            prefs.uploadLogs = uploadError.value!!
            uploadErrorTmp = uploadError.value!!
        }
    }

    fun checkPreferenceChanged(): Boolean {
        if (uploadErrorTmp != uploadError.value!!) {
            return true
        }
        return false
    }
}
